package cn.yl.vod.server.service.impl;

import cn.yl.vod.server.service.VodService;
import cn.yl.vod.server.util.AliyunConfig;
import com.alibaba.fastjson.JSON;
import com.aliyun.vod20170321.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aliyun.tea.*;
import com.aliyun.vod20170321.*;
import com.aliyun.vod20170321.models.*;
import com.aliyun.teaopenapi.*;
import com.aliyun.teaopenapi.models.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class VodServiceImpl implements VodService {

    @Autowired
    private AliyunConfig aliyunConfig;

    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "vod.cn-shanghai.aliyuncs.com";
        return new Client(config);
    }

    @Override
    public String getVodUrlByVodId(String vodId) throws Exception {
//        url播放
//        com.aliyun.vod20170321.Client client = VodServiceImpl.createClient(aliyunConfig.getAccessKeyId(), aliyunConfig.getAccessKeySecret());
//        GetPlayInfoRequest getPlayInfoRequest = new GetPlayInfoRequest()
//                .setVideoId("2a3ec0ebdbb64de2a54f7348593ee2ab");
//        // 复制代码运行请自行打印 API 的返回值
//        GetPlayInfoResponse playInfo = client.getPlayInfo(getPlayInfoRequest);
//        //fastJson 目前转换失败
//        GetPlayInfoResponseBody.GetPlayInfoResponseBodyPlayInfoListPlayInfo getPlayInfoResponseBodyPlayInfoListPlayInfo = playInfo.getBody().getPlayInfoList().getPlayInfo().get(0);
//        String playURL = getPlayInfoResponseBodyPlayInfoListPlayInfo.playURL;
//        return playURL;
        //获取凭证
        Client client = VodServiceImpl.createClient(aliyunConfig.getAccessKeyId(), aliyunConfig.getAccessKeySecret());
        GetVideoPlayAuthRequest getVideoPlayAuthRequest = new GetVideoPlayAuthRequest()
                .setVideoId(vodId).setAuthInfoTimeout(200L);
        GetVideoPlayAuthResponse videoPlayAuth = client.getVideoPlayAuth(getVideoPlayAuthRequest);
        String playAuth = videoPlayAuth.getBody().getPlayAuth();
        return playAuth;
    }
}
